package com.fau.wmbj.server.controller;

import com.fau.wmbj.server.flowable.domain.bo.ProcessInstanceBo;
import com.fau.wmbj.server.flowable.domain.bo.ProcessInvalidBo;
import com.fau.wmbj.server.flowable.domain.bo.TaskUrgingBo;
import com.fau.wmbj.server.flowable.service.ActProcessInstanceService;
import com.fau.wmbj.server.idempotent.annotation.RepeatSubmit;
import com.fau.wmbj.server.utils.Message;
import com.fau.wmbj.server.utils.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 流程实例管理 控制层
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/processInstance")
public class ActProcessInstanceController {

    private final ActProcessInstanceService actProcessInstanceService;

    /**
     * 分页查询正在运行的流程实例
     */
    @GetMapping("/getPageByRunning")
    public Message getPageByRunning(@RequestBody ProcessInstanceBo bo, @RequestBody Pageable pageable) {
        return Message.success(actProcessInstanceService.getPageByRunning(bo, pageable));
    }

    /**
     * 分页查询已结束的流程实例
     *
     * @param bo 参数
     */
    @GetMapping("/getPageByFinish")
    public Message getPageByFinish(ProcessInstanceBo bo, Pageable pageable) {
        return Message.success(actProcessInstanceService.getPageByFinish(bo, pageable));
    }

    /**
     * 通过流程实例id获取历史流程图
     *
     * @param processInstanceId 流程实例id
     */
    @GetMapping("/getHistoryImage/{processInstanceId}")
    public Message getHistoryImage(@PathVariable String processInstanceId) {
        return Message.success("操作成功", actProcessInstanceService.getHistoryImage(processInstanceId));
    }

    /**
     * 通过流程实例id获取历史流程图运行中，历史等节点
     *
     * @param processInstanceId 流程实例id
     */
    @GetMapping("/getHistoryList/{processInstanceId}")
    public Message getHistoryList(@PathVariable String processInstanceId) {
        return Message.success("操作成功", actProcessInstanceService.getHistoryList(processInstanceId));
    }

    /**
     * 获取审批记录
     *
     * @param processInstanceId 流程实例id
     */
    @GetMapping("/getHistoryRecord/{processInstanceId}")
    public Message getHistoryRecord(@PathVariable String processInstanceId) {
        return Message.success(actProcessInstanceService.getHistoryRecord(processInstanceId));
    }

    /**
     * 作废流程实例，不会删除历史记录(删除运行中的实例)
     *
     * @param processInvalidBo 参数
     */
    @RepeatSubmit()
    @PostMapping("/deleteRunInstance")
    public Message deleteRunInstance(@RequestBody ProcessInvalidBo processInvalidBo) {
        return Message.success(actProcessInstanceService.deleteRunInstance(processInvalidBo));
    }

    /**
     * 运行中的实例 删除程实例，删除历史记录，删除业务与流程关联信息
     *
     * @param processInstanceIds 流程实例id
     */
    @RepeatSubmit()
    @DeleteMapping("/deleteRunAndHisInstance/{processInstanceIds}")
    public Message deleteRunAndHisInstance(@PathVariable String[] processInstanceIds) {
        return Message.success(actProcessInstanceService.deleteRunAndHisInstance(Arrays.asList(processInstanceIds)));
    }

    /**
     * 已完成的实例 删除程实例，删除历史记录，删除业务与流程关联信息
     *
     * @param processInstanceIds 流程实例id
     */
    @RepeatSubmit()
    @DeleteMapping("/deleteFinishAndHisInstance/{processInstanceIds}")
    public Message deleteFinishAndHisInstance(@PathVariable String[] processInstanceIds) {
        return Message.success(actProcessInstanceService.deleteFinishAndHisInstance(Arrays.asList(processInstanceIds)));
    }

    /**
     * 撤销流程申请
     *
     * @param processInstanceId 流程实例id
     */
    @RepeatSubmit()
    @PostMapping("/cancelProcessApply/{processInstanceId}")
    public Message cancelProcessApply(@PathVariable String processInstanceId) {
        return Message.success(actProcessInstanceService.cancelProcessApply(processInstanceId));
    }

    /**
     * 分页查询当前登录人单据
     *
     * @param bo 参数
     */
    @GetMapping("/getPageByCurrent")
    public Message getPageByCurrent(ProcessInstanceBo bo, Pageable pageable) {
        return Message.success(actProcessInstanceService.getPageByCurrent(bo, pageable));
    }

    /**
     * 任务催办(给当前任务办理人发送站内信，邮件，短信等)
     *
     * @param taskUrgingBo 任务催办
     */
    @RepeatSubmit()
    @PostMapping("/taskUrging")
    public Message taskUrging(@RequestBody TaskUrgingBo taskUrgingBo) {
        return Message.success(actProcessInstanceService.taskUrging(taskUrgingBo));
    }

}
